#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;

namespace Osh.Dvb
{
	/// <summary>
	/// Defines a base class for program or program element descriptors.
	/// Program and program element descriptors are structures which may be used to extend the definitions of
	/// programs and program elements. All descriptors have a format which begins with an 8 bit tag value.
	/// The tag value is followed by an 8 bit descriptor length and data fields.
	/// See ISO/IEC 13818-1 section 2.6 
	/// </summary>
	public class Descriptor : IDescriptor
	{
		public const int HeaderLength = 2;
		public const int MaxBodyLength = byte.MaxValue - HeaderLength;

		private DescriptorTags _tag;
		private byte _bodyLength;

		public Descriptor(byte tag, int bodyLength)
			: this((DescriptorTags)tag, bodyLength)
		{
		}

		public Descriptor(DescriptorTags tag, int bodyLength)
		{
			if(bodyLength < byte.MinValue || bodyLength > MaxBodyLength)
				throw new ArgumentOutOfRangeException("bodyLength");

			_tag = tag;
			_bodyLength = (byte)bodyLength;
		}

		public DescriptorTags Tag
		{
			get { return _tag; }
			set { _tag = value; }
		}

		public byte BodyLength
		{
			get { return _bodyLength; }
			set
			{
				if(value > MaxBodyLength)
					throw new ArgumentOutOfRangeException("value");

				_bodyLength = value;
			}
		}

		public virtual int Length
		{
			get { return HeaderLength + BodyLength; }
		}

	}
}
